package cn.bytepath.org.service.impl;

import cn.bytepath.annotation.Description;
import cn.bytepath.base.mapper.BaseMapper;
import cn.bytepath.base.service.impl.BaseServiceImpl;
import cn.bytepath.org.domain.Department;
import cn.bytepath.org.mapper.DepartmentMapper;
import cn.bytepath.org.query.DepartmentQuery;
import cn.bytepath.org.service.IDepartmentService;
import cn.bytepath.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 部门业务层的实现类
 * @author Jack.C
 * @createDate 2022-01-18
 * @version 1.0
 */
@Description("部门")
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    /**
     * 注入部门mapper对象
     */
    @Autowired
    private DepartmentMapper departmentMapper;



    /**
     * 根据是否存在id决定是新增(id为null)还是修改(id不为null)
     * @param department 需保存或者修改的数据的存值对象
     * @update 2022/1/18
     */
    @Transactional
    @Override
    public void saveOrUpdate(Department department) {

        // save后返回了主键在department对象里面,所以可以重新setDirpath
        if(department.getId() == null) {
            departmentMapper.save(department);
            saveOrUpdate(department);
        } else {
            if(department.getParent_id() == null) {
                department.setDirPath("/" + department.getId());
            } else {
                Department parent = departmentMapper.findById(department.getParent_id());
                department.setDirPath(parent.getDirPath() + "/" + department.getId());
            }
            departmentMapper.update(department);
        }
    }


    /**
     * 查询部门信息的文档树,即所有的部门以及其对应的子部门信息
     * @return: java.util.List<cn.bytepath.org.domain.Department>
     * @update 2022/1/23
     */
    @Override
    @Description("#模块,查询部门的文档树信息")
    public List<Department> findDeptTree() {

        return departmentMapper.findDeptTree();
    }
}
